<template>
	<view class="container">
		<view class="header f-28 col-3">
			<view @tap="bindHeaderTap" :class="{ active: dataType === 'all' }" data-type="all"><text>全部</text></view>
			<view @tap="bindHeaderTap" :class="{ active: dataType === 'payment' }" data-type="payment"><text>待付款</text></view>
			<view @tap="bindHeaderTap" :class="{ active: dataType === 'sharing' }" data-type="sharing"><text>拼团中</text></view>
			<view @tap="bindHeaderTap" :class="{ active: dataType === 'delivery' }" data-type="delivery"><text>待发货</text></view>
			<view @tap="bindHeaderTap" :class="{ active: dataType === 'received' }" data-type="received"><text>待收货</text></view>
			<view @tap="onTargetRefund"><text>售后</text></view>
		</view>
		<view class="goods-detail-box">
			<scroll-view @scrolltolower="bindDownLoad" scroll-y :style="{ height: scrollHeight + 'px', width: '750rpx' }">
				<view class="wrapper" v-if="list.data.length > 0">
					<view class="m-top20" v-for="(item, index) in list.data" :key="index">
						<form @submit="navigateToDetail" report-submit="true" :data-id="item.order_id">
							<button class="btn-normal" formType="submit">
								<!-- 订单状态 -->
								<view class="order-box b-f">
									<view class="order-header dis-flex">
										<text class="col-m">{{ item.state_text }}</text>
									</view>
								</view>
								<!-- 商品信息 -->
								<view class="order-box order-img_list">
									<view class="detail-goods b-f dis-flex flex-dir-row">
										<view class="left"><image class="goods-image" :src="item.goods[0].image.file_path"></image></view>
										<view class="right dis-flex flex-box flex-dir-column flex-x-around">
											<view class="goods-name">
												<text class="twolist-hidden">{{ item.goods[0].goods_name }}</text>
											</view>
											<view class="dis-flex col-9 f-24">
												<view class="flex-box">{{ item.goods[0].goods_attr }}</view>
												<text class="t-r">×{{ item.goods[0].total_num }}</text>
											</view>
										</view>
									</view>
								</view>
							</button>
							<!-- 底部区 -->
							<view class="order-box b-f">
								<view class="order-btn dis-flex">
									<view class="order-left dis-flex flex-y-center">
										<view class="text">
											<text>合计：</text>
											<text class="col-m">￥{{ item.pay_price }}</text>
										</view>
									</view>
									<view class="order-right flex-box">
										<view class="btn-group clear" v-if="item.order_status.value != 20">
											<!-- 未支付取消订单 -->
											<view v-if="item.pay_status.value == 10" class="btn-group-item"><view @tap="cancelOrder" class="btn-default" :data-id="item.order_id">取消</view></view>
											<!-- 已支付取消订单 -->
											<view v-if="item.order_status.value != 21 && item.order_type.value == 10 && item.pay_status.value == 20 && item.delivery_status.value == 10" class="btn-group-item"><view @tap="cancelOrder" class="btn-default" :data-id="item.order_id">申请取消</view></view>
											<view v-if="item.order_status.value == 21" class="f-28 col-8 t-r">取消申请中</view>
											<!-- 订单付款 -->
											<view v-if="item.pay_status.value == 10" class="btn-group-item"><view @tap="onPayOrder" class="btn-main" :data-id="item.order_id">付款</view></view>
											<!-- 订单核销码 -->
											<view
												v-if="item.pay_status.value == 20 && item.delivery_type.value == DeliveryTypeEnum.EXTRACT.value && item.delivery_status.value == 10 && (item.order_type.value == 20 ? item.active.status.value == 20 : true) && item.order_status.value != 21"
												class="btn-group-item"
											>
												<view @tap="onExtractQRCode" class="btn-main" :data-id="item.order_id">
													<text class="iconfont icon-qr-extract"></text>
													<text class="m-l-10">核销码</text>
												</view>
											</view>
											<!-- 确认收货 -->
											<view v-if="item.delivery_status.value == 20 && item.receipt_status.value == 10" class="btn-group-item"><view @tap="receipt" class="btn-main" :data-id="item.order_id">确认收货</view></view>
											<!-- 订单评价 -->
											<view v-if="item.order_status.value == 30 && item.is_comment == 0" class="btn-group-item"><view @tap="comment" class="btn-default" :data-id="item.order_id">评价</view></view>
											<!-- 拼单详情 -->
											<view v-if="item.order_type.value == 20 && item.pay_status.value == 20" class="btn-group-item">
												<form @submit="navigateToSharingActive" report-submit="true" :data-id=" item.active_id ">
													<button class="btn-normal" formType="submit"><view class="btn-default">拼单详情</view></button>
												</form>
											</view>
										</view>
									</view>
								</view>
							</view>
						</form>
					</view>
				</view>
				<view v-if="no_more" class="no-more f-30">亲, 没有更多了</view>
				<view v-if="!isLoading && !list.data.length">
					<view class="yoshop-notcont">
						<text class="iconfont icon-wushuju"></text>
						<text class="cont">亲，您还没有订单信息哦！</text>
						<navigator url="../../sharing/index/index">去开团</navigator>
					</view>
				</view>
			</scroll-view>
		</view>
		<!-- 核销码弹出层 -->
		<popup :show="showQRCodePopup" @close="onToggleQRCodePopup">
			<view class="pop-qrcode pop-example__container">
				<view class="pop-title"><text class="f-30">核销二维码</text></view>
				<view class="pop-content"><image :src="QRCodeImage"></image></view>
			</view>
		</popup>
		<!-- 支付方式弹窗 -->
		<popup :show="showPayPopup" @close="onTogglePayPopup">
			<view class="pop-orderPay pop-example__container">
				<view class="pop-title"><text class="f-30">请选择支付方式</text></view>
				<view class="pop-content">
					<view class="pay-method">
						<form @submit="onSelectPayType" :data-value="PayTypeEnum.WECHAT.value" report-submit="true" v-if="paymentChannel.WECHAT">
							<button formType="submit" class="btn-normal">
								<view class="pay-item dis-flex flex-x-between">
									<view class="item-left dis-flex flex-y-center">
										<view class="item-left_icon wechat"><text class="iconfont icon-weixinzhifu"></text></view>
										<view class="item-left_text">
											<text>{{ PayTypeEnum.WECHAT.name }}</text>
										</view>
									</view>
								</view>
							</button>
						</form>
						<form @submit="onSelectPayType" :data-value="PayTypeEnum.ALIPAY.value" report-submit="true" v-if="paymentChannel.ALIPAY">
							<button formType="submit" class="btn-normal">
								<view class="pay-item dis-flex flex-x-between">
									<view class="item-left dis-flex flex-y-center">
										<view class="item-left_icon alipay"><text class="iconfont icon-zhifubao"></text></view>
										<view class="item-left_text">
											<text>{{ PayTypeEnum.ALIPAY.name }}</text>
										</view>
									</view>
								</view>
							</button>
						</form>
						<form @submit="onSelectPayType" :data-value="PayTypeEnum.BALANCE.value" report-submit="true" v-if="paymentChannel.BALANCE">
							<button formType="submit" class="btn-normal">
								<view class="pay-item dis-flex flex-x-between">
									<view class="item-left dis-flex flex-y-center">
										<view class="item-left_icon balance"><text class="iconfont icon-qiandai"></text></view>
										<view class="item-left_text">
											<text>{{ PayTypeEnum.BALANCE.name }}</text>
										</view>
									</view>
								</view>
							</button>
						</form>
					</view>
				</view>
			</view>
		</popup>
	</view>
</template>

<script>
// 枚举类：发货方式
import DeliveryTypeEnum from '@/utils/enum/DeliveryType.js';

// 枚举类：支付方式
import PayTypeEnum from '@/utils/enum/order/PayType';

import Tools from '@/utils/Tools.js';
import Route from '@/utils/Route.js';
import popup from '@/components/popup/index.vue';
import Payment from '@/utils/Payment.js';
export default {
	components: {
		popup
	},
	data() {
		return {
			dataType: 'all', // 列表类型
			list: { data: [] }, // 订单列表
			scrollHeight: null, // 列表容器高度

			DeliveryTypeEnum, // 配送方式
			PayTypeEnum, // 支付方式

			no_more: false, // 没有更多数据
			isLoading: true, // 是否正在加载中

			page: 1, // 当前页码

			showQRCodePopup: false, // 核销码弹窗显示隐藏
			QRCodeImage: '', // 核销码图片
			showPayPopup: false,
			authId: null,
			paymentChannel: {}
		};
	},
	onLoad(options) {
		// 设置scroll-view高度
		this.setListHeight();
		// 设置数据类型
		this.dataType = options.type || 'all';
		this.authId = uni.getStorageSync('authId') || null;
		const paymentFilter = Payment.filter();
		this.paymentChannel = paymentFilter.paymentChannel;
	},
	onShow() {
		// 获取订单列表
		this.getOrderList(this.dataType);
	},
	methods: {
		/**
		 * 获取订单列表
		 */
		getOrderList(isPage, page) {
			this.$get(this.$api.getSharingOrderLists, { page: page || 1, dataType: this.dataType }, res => {
				let resList = res.data.list;
				let dataList = this.list;
				if (isPage == true) {
					this.$set(this.list, 'data', dataList.data.concat(resList.data));
					this.isLoading = false;
				} else {
					this.list = resList;
					this.isLoading = false;
				}
			});
		},
		/**
		 * 切换标签
		 */
		bindHeaderTap(e) {
			this.dataType = e.currentTarget.dataset.type;
			this.list = { data: [] };
			this.isLoading = true;
			this.page = 1;
			this.no_more = false;
			this.getOrderList(e.currentTarget.dataset.type);
		},
		/**
		 * 取消订单
		 */
		cancelOrder(e) {
			let _this = this;
			let order_id = e.currentTarget.dataset.id;
			uni.showModal({
				title: '友情提示',
				content: '确认要取消该订单吗？',
				success(o) {
					if (o.confirm) {
						this.$post(this.$api.cancelSharingOrder, { order_id }, res => {
							this.getOrderList(this.dataType);
						});
					}
				}
			});
		},
		/**
		 * 确认收货
		 */
		receipt(e) {
			let order_id = e.currentTarget.dataset.id;
			uni.showModal({
				title: '提示',
				content: '确认收到商品？',
				success(o) {
					if (o.confirm) {
						this.$post(this.$api.receiptSharingOrder, { order_id }, res => {
							this.getOrderList(this.dataType);
						});
					}
				}
			});
		},
		/**
		 * 点击付款按钮
		 */
		onPayOrder(e) {
			// 记录订单id
			this.payOrderId = e.currentTarget.dataset.id;
			this.onTogglePayPopup();
		},
		/**
		 * 选择支付方式
		 */
		onSelectPayType(e) {
			// 记录formId
			this.$saveFormId(e.detail.formId);
			// 隐藏支付方式弹窗
			this.onTogglePayPopup();
			if (!this.showPayPopup) {
				// 发起付款请求
				this.payment(this.payOrderId, e.currentTarget.dataset.value);
			}
		},
		/**
		 * 显示/隐藏支付方式弹窗
		 */
		onTogglePayPopup() {
			this.showPayPopup = !this.showPayPopup;
		},
		/**
		 * 发起付款请求
		 */
		payment(orderId, payType) {
			// 显示loading
			uni.showLoading({
				title: '正在处理...'
			});
			const postData = {
				order_id: orderId,
				payType: payType
			};
			if (this.authId) {
				postData.id = this.authId;
			}
			this.$post(
				this.$api.paySharingOrder,
				postData,
				res => {
					if (res.code === -10) {
						Tools.showError(res.msg);
						return false;
					}

					// 余额支付
					if (res.data.pay_type == PayTypeEnum.BALANCE.value) {
						Tools.showSuccess(res.msg.success, () => {
							// 跳转到已付款订单
							Route.navigateTo({
								url: '../order/detail/detail?order_id=' + orderId
							});
						});
					} else {
						Payment.pay(
							result.data.payment,
							result.data.pay_type,
							() => {
								this.redirectToOrderIndex();
							},
							msg => {
								this.redirectToOrderIndex();
							}
						);
					}
				},
				null,
				() => {
					uni.hideLoading();
				}
			);
		},
		/**
		 * 订单评价
		 */
		comment(e) {
			let order_id = e.currentTarget.dataset.id;
			wx.navigateTo({
				url: './comment/comment?order_id=' + order_id
			});
		},
		/**
		 * 跳转订单详情页
		 */
		navigateToDetail(e) {
			// 记录formId
			this.$saveFormId(e.detail.formId);
			let order_id = e.currentTarget.dataset.id;
			Route.navigateTo({
				url: './detail/detail?order_id=' + order_id
			});
		},
		/**
		 * 跳转到拼团详情
		 */
		navigateToSharingActive(e) {
			// 记录formId
			this.$saveFormId(e.detail.formId);
			let active_id = e.currentTarget.dataset.id;
			Route.navigateTo({
				url: '../active/index?active_id=' + active_id
			});
		},

		/**
		 * 跳转到售后列表
		 */
		onTargetRefund() {
			Route.navigateTo({
				url: './refund/index'
			});
		},
		/**
		 * 下拉到底加载数据
		 */
		bindDownLoad() {
			// 已经是最后一页
			if (this.page >= this.list.last_page) {
				this.no_more = true;
				return false;
			}
			// 加载下一页列表
			this.getOrderList(true, ++this.page);
		},
		/**
		 * 设置商品列表高度
		 */
		setListHeight() {
			let systemInfo = wx.getSystemInfoSync();
			let tapHeight = uni.upx2px(90); // tap高度
			let scrollHeight = systemInfo.windowHeight - tapHeight; // swiper高度
			this.scrollHeight = scrollHeight;
		}
	}
};
</script>

<style>
.header {
	width: 100%;
	height: 88rpx;
	border-bottom: 1rpx solid #e4e4e4;
	display: flex;
	background: #fff;
	line-height: 88rpx;
	position: sticky;
	top: 0;
	z-index: 10;
}

.header view {
	flex: 1;
	text-align: center;
	padding: 0 15rpx;
}

.header view text {
	display: block;
}

.header view.active {
	color: #fd4a5f;
}

.header view.active text {
	border-bottom: 1px solid #fd4a5f;
}

/* 商品列表 */

.goods-detail-box {
	font-size: 28rpx;
}

.order-box {
	padding: 0 25rpx;
}

.order-header {
	padding: 18rpx 0;
}

.order-cont {
	padding: 15rpx 0;
	justify-content: space-between;
}

.order-cont .order-num text,
.order-cont .order-time text {
	color: #777;
	font-size: 26rpx;
}

.order-shop {
	padding: 16rpx 0 16rpx 0;
}

.order-shop-left,
.order-shop-left image {
	width: 160rpx;
	height: 160rpx;
	/* border: 1rpx solid #e4e4e4; */
	margin-right: 15rpx;
	display: inline-block;
	background: #fff;
}

.order-shop .num {
	float: right;
}

.order-btn {
	padding: 18rpx 0 18rpx 0;
	z-index: 9;
}

.order-btn .order-left text,
.order-btn .order-left .text {
	font-size: 29rpx;
}

.order-btn .order-left .text {
	color: #888;
}

.order-btn .order-right .btn-group {
	margin-bottom: -10rpx;
}

.order-btn .order-right .btn-group .btn-group-item {
	margin: 0 0 10rpx 10rpx;
	float: right;
}

.order-btn .order-right .btn-group .btn-group-item .btn-default {
	border-radius: 4px;
	border: 1rpx solid #ccc;
	padding: 6rpx 20rpx;
	font-size: 28rpx;
	color: #555;
}

.order-btn .order-right .btn-group .btn-group-item .btn-main {
	border-radius: 4px;
	border: 1rpx solid #fd4a5f;
	padding: 6rpx 20rpx;
	font-size: 28rpx;
	color: #fd4a5f;
}

.order-list-box .left {
	flex: 4;
	overflow: hidden;
	position: relative;
}

.order-list-box .right {
	flex: 1;
}

.goods-number {
	font-size: 28rpx;
	color: #777;
}

.user-orderJtou {
	font-size: 24rpx;
}

.order-img_list {
	background: #fff;
	border-top: 1rpx solid #f8f8f8;
	border-bottom: 1rpx solid #f8f8f8;
}

/* 商品详情 */

.detail-goods {
	padding: 18rpx;
}

.detail-goods .left .goods-image {
	display: block;
	width: 150rpx;
	height: 150rpx;
}

.detail-goods .right {
	padding-left: 25rpx;
}

.detail-goods .right .goods-name {
	color: #333;
}

.yoshop-notcont navigator {
	background: #fd4a5f;
	height: 30px;
	line-height: 30px;
	font-size: 28rpx;
	width: 200rpx;
	margin: 0 auto;
	border-radius: 5px;
	text-align: center;
	color: #f8f8f8;
	margin-top: 25rpx;
}

/* 弹出层 */

.pop-example__container {
	background: #fff;
	padding: 30rpx 40rpx;
	border-radius: 10rpx;
	min-width: 460rpx;
}

.pop-example__container .pop-title {
	text-align: center;
}

.pop-content {
	margin-top: 30rpx;
}

/* 弹出层：核销码 */

.pop-qrcode .pop-content image {
	display: block;
	width: 460rpx;
	height: 460rpx;
}

/* 弹出层：支付方式 */

.pop-orderPay .pop-title {
	text-align: left;
}

.pay-method .pay-item {
	padding: 20rpx 0;
	font-size: 28rpx;
	/* border-bottom: 1rpx solid rgb(248, 248, 248); */
}

.pay-method .pay-item .item-left_icon {
	margin-right: 20rpx;
	font-size: 32rpx;
}

.pay-method .pay-item .item-left_icon.wechat {
	color: #00c800;
}

.pay-method .pay-item .item-left_icon.balance {
	color: #ff9700;
}
.pay-method .pay-item .item-left_icon.alipay {
	color: #0093e2;
}
</style>
